package com.bayihouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bayihouse.constant.CommonConstant;
import com.bayihouse.mapper.HouseMapper;
import com.bayihouse.model.entity.House;
import com.bayihouse.model.request.house.HouseQueryRequest;
import com.bayihouse.model.vo.HouseVO;
import com.bayihouse.service.HouseService;
import com.bayihouse.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【house】的数据库操作Service实现
 * @createDate 2024-09-17 10:28:03
 */
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House>
        implements HouseService {

    @Override
    public HouseVO getHouseVO(House house) {
        if (house == null) {
            return null;
        }
        return HouseVO.objToVo(house);
    }

    @Override
    public QueryWrapper<House> getQueryWrapper(HouseQueryRequest houseQueryRequest) {
        Long id = houseQueryRequest.getId();
        String name = houseQueryRequest.getName();
        Integer probeNum = houseQueryRequest.getProbeNum();
        String detail = houseQueryRequest.getDetail();
        Long communityId = houseQueryRequest.getCommunityId();
        String sortField = houseQueryRequest.getSortField();
        String sortOrder = houseQueryRequest.getSortOrder();
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.eq(probeNum != null, "probeNum", probeNum);
        queryWrapper.eq(StringUtils.isNotBlank(detail), "detail", detail);
        queryWrapper.eq(communityId != null, "father_id", communityId);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_DESC),
                sortField);
        return queryWrapper;
    }

    @Override
    public List<HouseVO> getHouseVOList(List<House> records) {
        if (CollectionUtils.isEmpty(records)) {
            return new ArrayList<>();
        }
        return records.stream().map(this::getHouseVO).collect(Collectors.toList());
    }

    @Override
    public Long getByCommunityIdAndAddress(Long communityId, String address) {
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(communityId != null, "communityId", communityId);
        queryWrapper.like(StringUtils.isNotBlank(address), "name", address);
        return this.getOne(queryWrapper).getId();
    }
}




